package com.xuzhao.handle;

import com.xuzhao.entity.Result;
import com.xuzhao.enums.ResultEnum;
import com.xuzhao.exception.PersonException;
import com.xuzhao.utils.ResultUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by xz on 2017/9/3.
 * 这里完成了异常的捕获 对抛出的异常进行处理
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
    @ExceptionHandler(value = Exception.class)
    @ResponseBody//由于返回Json 类也没有RestController
    public Result handle(Exception e){
        if (e instanceof PersonException){
            return ResultUtil.eror(((PersonException) e).getCode(),e.getMessage());
        }else{
            logger.error("【系统异常】{}",e);
            return ResultUtil.eror(ResultEnum.UNKONW_ERROR.getCode(),ResultEnum.UNKONW_ERROR.getMsg());
        }
    }
}
